package com.ruoyi.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.TimeZone;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.domain.T03;
import com.ruoyi.domain.T06;
import com.ruoyi.domain.T10VO;
import com.ruoyi.common.result.Result;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.service.IT03Service;
import com.ruoyi.service.IT06Service;
import com.ruoyi.service.IT18Service;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.T10;
import com.ruoyi.service.IT10Service;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 充值记录Controller
 *
 * @author ruoyi
 * @date 2024-07-29
 */
@RestController
@RequestMapping("/payrecord/record")
public class T10Controller extends BaseController
{
    @Autowired
    private IT10Service t10Service;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IT18Service t18Service;
    @Autowired
    private IT03Service t03Service;
    @Autowired
    private IT06Service t06Service;

    /**
     * 查询充值记录列表
     */
    @PreAuthorize("@ss.hasPermi('payrecord:record:list')")
    @GetMapping("/list")
    @Log(title = "充值")
    public TableDataInfo list(T10 t10)
    {
        startPage();
        t10.setF1006(1L);
        List<T10> list = t10Service.selectT10List(t10);
        return getDataTable(list);
    }

    /**
     * 导出充值记录列表
     */
    @Log(title = "充值记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, T10 t10,HttpServletRequest request) throws ParseException {

        if(!ObjectUtils.isEmpty(t10.getStartDate())){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            t10.setStartDate(sdf.parse(sdf.format(t10.getStartDate())));
            t10.setEndDate(sdf.parse(sdf.format(t10.getEndDate())));
        }
        LoginUser loginUser =tokenService.getLoginUser(request);
        Long userId=loginUser.getUserId();
        Long ComId=t18Service.selectF0301ByF1801(userId);
        t10.setF0301(ComId);
        List<T10> list = t10Service.selectT10ListNoPage(t10);
        ExcelUtil<T10> util = new ExcelUtil<T10>(T10.class);
        util.exportExcel(response, list, "充值记录数据");
    }

    /**
     * 获取充值记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('payrecord:record:query')")
    @Log(title = "充值")
    @GetMapping(value = "/{F1001}")
    public AjaxResult getInfo(@PathVariable("F1001") Long F1001)
    {
        return success(t10Service.selectT10ByF1001(F1001));
    }

    /**
     * 新增充值记录
     */
    @PreAuthorize("@ss.hasPermi('payrecord:record:add')")
    @Log(title = "充值记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody T10 t10)
    {
        return toAjax(t10Service.insertT10(t10));
    }

    /**
     * 修改充值记录
     */
    @PreAuthorize("@ss.hasPermi('payrecord:record:edit')")
    @Log(title = "充值记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody T10 t10)
    {
        return toAjax(t10Service.updateT10(t10));
    }

    /**
     * 删除充值记录
     */
    @PreAuthorize("@ss.hasPermi('payrecord:record:remove')")
    @Log(title = "充值记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{F1001s}")
    public AjaxResult remove(@PathVariable Long[] F1001s)
    {
        return toAjax(t10Service.deleteT10ByF1001s(F1001s));
    }

    @GetMapping("/getPayRecords")
    @ApiOperation("公司充值记录")
    public Result getPayRecords(Long F0301) throws  Exception{
        if (F0301 == null){
            return new Result("参数错误");
        }
        List<T10VO> list = t10Service.getPayRecords(F0301);
        if (list.size() == 0){
            return new Result("没有充值记录");
        }
        return Result.success(list,"success");
    }

    @GetMapping("/getPAyRecordPeople")
    @Log(title = "充值")
    @ApiOperation("充值人")
    public Result getPAyRecordPeople(Long F0601) throws Exception{
        if (F0601 == null){
            return new Result("参数错误");
        }
        List<T10VO> list = t10Service.getPAyRecordPeople(F0601);
        if (list.size() == 0){
            return new Result("没有充值记录");
        }
        return Result.success(list,"success");
    }
    @GetMapping("/CompyRecord")
    @Log(title = "充值")
    public TableDataInfo getCompyRecord(T10 t10,HttpServletRequest request) throws Exception{
        if(!ObjectUtils.isEmpty(t10.getStartDate())){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            t10.setStartDate(sdf.parse(sdf.format(t10.getStartDate())));
            t10.setEndDate(sdf.parse(sdf.format(t10.getEndDate())));
        }
        LoginUser loginUser =tokenService.getLoginUser(request);
        Long userId=loginUser.getUserId();
        Long ComId=t18Service.selectF0301ByF1801(userId);
        t10.setF0301(ComId);
        t10.setF1006(1L);
        t10.setF1010(0L);
        List<T10> list = t10Service.selectT10List(t10);
        return getDataTable(list);
    }

    @GetMapping("/getAccountMessage")
    @Log(title = "充值")
    public TableDataInfo getAccountMessage(T06 t06,HttpServletRequest request) throws Exception{
        Long ComId=getComId(request);
        t06.setF0301(ComId);
        List<T06> list = t06Service.selectT06List(t06);
        return getDataTable(list);
    }
    @ApiOperation("充值")
    @Log(title = "充值")
    @GetMapping("/recharge/{money}")
    public Result recharge(HttpServletRequest request ,@PathVariable("money") Long money){
        try {
            return t10Service.recharge(request,money);
        } catch (Exception e) {
            return Result.erro("初始化失败："+e.getMessage());
        }
    }

    @ApiOperation("充值回调")
    @PostMapping("/rechargeCallback")
    public  Result rechargeCallback(HttpServletRequest request) throws Exception {
        try {
            return t10Service.rechargeCallback(request);
        } catch (Exception e) {
            return Result.erro("充值失败"+e.getMessage());
        }
    }

    public Long getComId(HttpServletRequest request){
        LoginUser loginUser =tokenService.getLoginUser(request);
        Long userId=loginUser.getUserId();
        Long ComId=t18Service.selectF0301ByF1801(userId);
        return ComId;
    }

    @PostMapping("/excelExport")
    @Log(title = "充值")
    public void excelExport(HttpServletResponse response) throws IOException {
        t10Service.excelExport(response);
    }


    @GetMapping("/getDetail")
    @Log(title = "充值")
    public TableDataInfo getDetail(HttpServletRequest request,T10 t10){
        List<T10> list = t10Service.getDetail(request,t10);
        return getDataTable(list);
    }

    @GetMapping("/getMyRecord")
    @Log(title = "充值")
    public TableDataInfo getMyRecord(HttpServletRequest request,T10 t10){
        startPage();
        LoginUser loginUser =tokenService.getLoginUser(request);
        Long userId=loginUser.getUserId();
        t10.setF0601(userId);
        t10.setF1010(0L);
        t10.setF1006(0L);
        List<T10> list = t10Service.selectT10List(t10);
        return getDataTable(list);
    }
}
